package main

import "fmt"

func 整数类型() {
	var x int
	x = 18
	var y int8 //-128 到 127
	y = 127
	var z int16 //-32768 到 32767
	z = 32767
	var a int32
	a = 2147483647
	var b int64
	b = 9223372036854775807
	fmt.Println(x, y, z, a, b)
}

func 浮点类型() {
	var a float32
	a = 3.14
	fmt.Printf("a :%f \n ", a)

	var b float64
	fmt.Printf("b: %f \n", b)
}

func 布尔类型() {
	//布尔值
	var isOk bool = true
	isOk = false
	fmt.Printf("isOk: %t", isOk)
}

func 数组类型() {
	var arr = [5]int{1, 2, 3, 4, 5}
	for i := 0; i < len(arr); i++ {
		fmt.Println(arr[i])
	}
	// i 索引 v 值
	for i, v := range arr {
		fmt.Println(i, v)
	}

	//定义长度为5数组
	var arr2 [5]int
	arr2[0] = 1
	fmt.Println(arr2[0])

	var arr3 = make([]int, 5)
	fmt.Println(arr3)

	fmt.Printf("arr3 type is %T \n", arr3)

	//slice 切片
	var arr4 []int
	//arr4[0] = 1
	if arr4 == nil {
		fmt.Println("arr4 is nil")
	}
	arr4 = append(arr4, 1)
	fmt.Println(arr4)

	//make 分配内存
	var arr5 = make([]int, 5)
	arr5 = append(arr5, 11)
	fmt.Println(arr5)

	arr5[1] = 44
	arr5[2] = 33

	//切片截取
	fmt.Println(arr5[0:2])

	//	切片拷贝
	var arr6 = make([]int, len(arr5))
	//主要用于切片的拷贝
	copy(arr6, arr5[:])
	fmt.Println(arr6)

}

func 切片类型() {
	//	声明切片的方式
	//1 方式一
	var slice = []int{1, 2, 3, 4, 5}
	fmt.Printf("slice type is %T   value: %v \n ", slice, slice)
	// 方式2
	var slice2 []int
	//判断是否为nil
	if slice2 == nil {
		fmt.Println("slice2 is nil")
	}
	//切片的追加
	slice2 = append(slice2, 1)

	fmt.Printf("slice2 type is %T  value: %v \n ", slice2, slice2)

	//方式3 make初始长度为3，每次扩容5个
	var slice3 = make([]int, 3, 5)
	slice3 = append(slice3, 1)
	fmt.Printf("slice3 type is %T  value: %v \n ", slice3, slice3)

	//切片追加
	slice3 = append(slice3, 2)
	slice3 = append(slice3, 3)
	slice3 = append(slice3, 4)

	//截取
	fmt.Printf("截取前面两个元素:%v \n", slice3[0:2])
	fmt.Printf("截取第三个元素后面所有:%v \n", slice3[2:])
	fmt.Printf("截取所有元素:%v \n", slice3[:])
	fmt.Printf("截取第2到4的元素:%v \n", slice3[1:4])

}

func Map类型() {
	//声明 map，key是字符串，value是字符串
	//方式1
	var myMap map[string]string
	if myMap == nil {
		fmt.Println("myMap is nil")
	}
	//在使用map之前，必须先make分配空间 ，超出的容量会自动扩容
	myMap = make(map[string]string, 2)
	myMap["name"] = "张三"
	myMap["age"] = "18"
	myMap["sex"] = "男"
	fmt.Printf("myMap:%v \n", myMap)

	//方式2
	myMap2 := make(map[string]string)
	myMap2["name"] = "张三"
	fmt.Printf("myMap2:%v \n", myMap2)

	//方式3
	myMap3 := map[string]string{
		"name": "张三",
		"age":  "18",
		"sex":  "男",
	}
	fmt.Printf("myMap3:%v \n", myMap3)

	//map遍历
	for k, v := range myMap3 {
		fmt.Println(k, v)
	}

	//删除元素
	delete(myMap3, "name")

	//判断元素是否存在
	value, ok := myMap3["name"]
	if ok {
		fmt.Println(value)
	} else {
		fmt.Println("name 不存在")
	}

	//修改元素
	myMap3["age"] = "19"
	fmt.Printf("myMap3:%v \n", myMap3)
}

func main() {
	//布尔类型()
	//整数类型()
	//浮点类型()
	//数组类型()
	//切片类型()
	Map类型()
}
